#include <iostream>
#include <stack>

using namespace std;


class MyQueue {
private:
    stack<int> stackIn;
    stack<int> stackOut;

    void reset() {
        if (!stackOut.empty()) {
            return;
        }
        while (!stackIn.empty()) {
            stackOut.push(stackIn.top());
            stackIn.pop();
        }
    }
public:
    MyQueue() {
    }

    void push(int x) {
        stackIn.push(x);
    }

    int pop() {
        reset();
        int topElement = stackOut.top();
        stackOut.pop();
        return topElement;
    }

    int peek() {
        reset();
        return stackOut.top();
    }

    bool empty() {
        reset();
        return stackOut.empty();
    }
};


int main() {
    MyQueue* queue = new MyQueue();
    queue->push(1);
    queue->push(2);
    queue->push(3);
    queue->push(4);

    cout << "top: " << queue->peek() << "\n";
    queue->pop();
    queue->push(5);

    cout << "top: " << queue->peek() << "\n";
    queue->pop();
    cout << "top: " << queue->peek() << "\n";
    queue->pop();
    cout << "top: " << queue->peek() << "\n";
    queue->pop();
    cout << "top: " << queue->peek() << "\n";
    queue->pop();
    
    return 0;
}